home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / fractals / apfelkiste / apfelkiste2.0 / source / apfelkiste2.0src.lzh / Float.c < prev    next >
C/C++ Source or Header  |  1991-10-23  |  5KB  |  137 lines

  1. /************************************************************************/
  2. /* Float.c                                */
  3. /*                                    */
  4. /* Here are found all of the routines doing the math and control logic    */
  5. /* for "Apfelkiste" using regular float calculations. Even these should    */
  6. /* be faster than any other "normal" Mandelbrot set generating program    */
  7. /* that uses the standard line-by-line algorithm. There is a little    */
  8. /* more overhead but profiling the program shows that more than 90% of    */
  9. /* calculation time is done in the function that does the math, so    */
  10. /* every point that is saved from beeing evaluated means saved time.    */
  11. /************************************************************************/
  12.  
  13. #include <intuition/intuition.h>
  14. #include <graphics/gfx.h>
  15. #include <graphics/gfxmacros.h>
  16. #include <proto/intuition.h>
  17. #include <proto/graphics.h>
  18. #include <mffp.h>
  19. #include <math.h>
  20.  
  21. extern struct RastPort    *rp;
  22. extern int         maxiter;
  23. extern double         dx, dy, divergenz, rmin, imin;
  24. extern short         maxcol;
  25.  
  26. /* -------------------------------------------------------------------- */
  27. /* Evaluate color of a given point X0 on the complex number plane (hope    */
  28. /* this is the correct term) by the formula:                */
  29. /*                                    */
  30. /*                                    2                    */
  31. /*                            X    = X  + X                */
  32. /*                             n+1    n    0                */
  33. /*                                    */
  34. /* and counting the iterations until Xn exceedes an upper boundary    */
  35. /* "divergenz" or a maximum count "maxiter" is reached. The number of    */
  36. /* iterations performed determines the color of the point to set.    */
  37. /* -------------------------------------------------------------------- */
  38.  
  39. long Iter_FLOAT(double r, double i)
  40. {
  41.     int    count = 0;
  42.     double    x, p, q;
  43.  
  44.     p = r; q = i;
  45.     while ( r * r + i * i < divergenz && count++ < maxiter ) {
  46.         x = r;
  47.         r = (x + i) * (x - i) + p;
  48.         i = 2.0 * x * i + q;
  49.     }
  50.     if ( count >= maxiter ) {
  51.         return 0;
  52.     }
  53.     else {
  54.         return (long) count % maxcol;
  55.     }
  56. }
  57.  
  58. /* -------------------------------------------------------------------- */
  59. /* Determine whether a rectangular area is surrounded by all the same    */
  60. /* color. In case of this no point inside the area will get a different */
  61. /* color and thus there's no need to do any calculations for points    */
  62. /* inside.                                */
  63. /* This function is also used by the fixpoint version of the algorithm.    */
  64. /* -------------------------------------------------------------------- */
  65.  
  66. short __regargs Test(long xx1, long yy1, long xx2, long yy2)
  67. {
  68.     long i;
  69.     int  c;
  70.  
  71.     c = ReadPixel(rp, xx1, yy1);
  72.  
  73.     for ( i = xx2; i >= xx1; i-- ) {
  74.         if ( ReadPixel(rp, i, yy1) != c ) return FALSE;
  75.         if ( ReadPixel(rp, i, yy2) != c ) return FALSE;
  76.     }
  77.  
  78.     for ( i = yy2 - 1; i > yy1; i-- ) {
  79.         if ( ReadPixel(rp, xx1, i) != c ) return FALSE;
  80.         if ( ReadPixel(rp, xx2, i) != c ) return FALSE;
  81.     }
  82.     return TRUE;
  83. }
  84.  
  85. /* -------------------------------------------------------------------- */
  86. /* Recursive evaluation scheme for "Apfelkiste". Not very much glasnost    */
  87. /* because of diverse optimizations (?), but briefly it does this:    */
  88. /* Called with the corners of an already calculated rectangular frame    */
  89. /* it tests for identity of the color of all points on that frame.    */
  90. /* If yes, fine! Just fill the points inside the area with this color    */
  91. /* and return without further work.                    */
  92. /* If not, the area is halved and the function calls itself once for    */
  93. /* each half until the size of the area degenerates to zero or the    */
  94. /* first termination criterium jumps in.                */
  95. /* Watch the execution of the program and you'll see.            */
  96. /* -------------------------------------------------------------------- */
  97.  
  98. void __regargs Apfel_FLOAT(int x1, int y1, int x2, int y2)
  99. {
  100.     int xneu, yneu;
  101.     long i;
  102.     double help1, help2;
  103.  
  104.     if ( x2 - x1 > 1 && y2 - y1 > 1 ) {
  105.         if ( Test(x1, y1, x2, y2) ) {
  106.             SetAPen(rp, ReadPixel(rp, x1, y1));
  107.             RectFill(rp, x1, y1, x2, y2);
  108.         }
  109.         else {
  110.             if ( x2 - x1 > y2 - y1 ) {
  111.                 xneu = ( x1 + x2 ) >> 1;
  112.                 help1 = rmin + xneu * dx;
  113.                 help2 = ( y1 + 1 ) * dy + imin;
  114.                 for ( i = y1 + 1; i < y2; i++ ) {
  115.                     SetAPen(rp, Iter_FLOAT(help1, help2));
  116.                     WritePixel(rp, xneu, i);
  117.                     help2 += dy;
  118.                 }
  119.                 Apfel_FLOAT(x1, y1, xneu, y2);
  120.                 Apfel_FLOAT(xneu, y1, x2, y2);
  121.             }
  122.             else {
  123.                 yneu = ( y1 + y2 ) >> 1;
  124.                 help1 = imin + yneu * dy;
  125.                 help2 = ( x1 + 1 ) * dx + rmin;
  126.                 for ( i = x1+1; i < x2; i++ ) {
  127.                     SetAPen(rp, Iter_FLOAT(help2, help1));
  128.                     WritePixel(rp, i, yneu);
  129.                     help2 += dx;
  130.                 }
  131.                 Apfel_FLOAT(x1, y1, x2, yneu);
  132.                 Apfel_FLOAT(x1, yneu, x2, y2);
  133.             }
  134.         }
  135.     }
  136. }
  137.